<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
    <title>Memorize Login</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: "微软雅黑", sans-serif;
            background-image: url(https://images.hdqwalls.com/wallpapers/5k-milky-way-jy.jpg);
            background-size: cover;
        }

        .container .tab {
            background-color: #98cfff;
            border: none;
            color: #017696;
            text-shadow: 2px 5px 4px #bf829c;
            letter-spacing: 2px;
            text-align: center;
            border-radius: 6px;
            width: 137px;
            font-size: 26px;
            font-weight: 700;
            text-align: center;
        }

        input {
            padding: 10px;
            width: 100%;
            color: white;
            margin-bottom: 10px;
            background-color: #555555;
            border: 1px solid black;
            border-radius: 4px;
            letter-spacing: 2px;
        }

        form button {
            width: 100%;
            padding: 10px;
            background-color: #b795a1;
            border: 1px solid black;
            border-radius: 4px;
            cursor: pointer;
        }

        .container {
            width: 400px;
            height: 400px;
            background-color: #98cfff;
            margin: 10% auto;
            color: rgb(255, 255, 255);
            border-radius: 20px;
            padding: 60px;
            transition: .5;
            opacity: 0.8;
        }

        .container h1 {
            text-align: center;
        }

        .container form {
            margin-top: 35px;
        }

        .container .form-group {
            margin-bottom: 10px;
            width: 100%;
            color: #ffffff;
        }

        
        .container .form-group .btn {
            border: 0;
            background-color: rgb(255, 255, 255);
            border-radius: 6px;
            margin-top: 10px;
            cursor: pointer;
        }

        .container .form-group input {
            border: none;
            background: transparent;
            border-bottom: 1px solid rgb(255, 255, 255);
            outline: none;
            padding: 5px;
            width: 100%;
        }


        #pagebg {
            /* background-color: #0088ff; */
            width: 500px;
            height: 400px;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
        }

        #title {
            color: white;
            position: absolute;
            width: 200px;
            height: 30px;
            top: 10px;
            right: 0;
            left: 0;
            margin: auto;
            text-align: center;
            font-size: 25px;
        }

        #ider {
            color: white;
            position: absolute;
            width: 200px;
            height: 30px;
            top: 100px;
            left: 20px;
            margin: auto;
            text-align: center;
            font-size: 20px;
        }

        #psd {
            color: white;
            position: absolute;
            width: 200px;
            height: 30px;
            top: 180px;
            left: 20px;
            margin: auto;
            text-align: center;
            font-size: 20px;
        }
    </style>

</head>

<body>
    <div class="container">
        <button class="tab" style="color:rgb(255, 255, 255);">注册</button>
        <button class="tab" style="color:rgb(255, 255, 255);">登录</button>
        <form action="#">
            <div class="form-group">
                <input id="tabcolor" type="text" style="color: #FFF;" placeholder="昵称"><br><br>
            </div>
            <div class="form-group">
                <input type="text" style="color: #FFF;" placeholder="用户名"><br><br>
            </div>
            <div class="form-group">
                <input type="password" style="color: #FFF;" placeholder="密码"><br><br>
            </div>
            <div class="form-group">
                <input type="submit" style="color: #017696;" class="btn"><br><br>
            </div>
        </form>
        <form action="#" style="display:none">
            <div class="form-group">
                <input type="text" style="color: #FFF;" placeholder="用户名"><br><br>
            </div>
            <div class="form-group">
                <input type="password" style="color: #FFF;" placeholder="密码"><br><br>
            </div>
            <div class="form-group">
                <input type="submit" style="color: #0088ff;" class="btn"><br><br>
            </div>
        </form>
    </div>


    <script>
        // 注册登录tab切换
        let tab = document.querySelectorAll('.tab');
        let container = document.querySelectorAll('form');
        for (let i = 0; i < tab.length; i++) {
            tab[i].setAttribute('index', i);
            tab[i].onclick = function () {
                for (let i = 0; i < tab.length; i++) {
                    tab[i].style.color = ''
                }
                this.style.color = '#fff';
                let index = this.getAttribute('index');
                for (let i = 0; i < container.length; i++) {
                    container[i].style.display = 'none';
                }
                container[index].style.display = 'block';
            }
        }

        const myHeaders = new Headers()
        myHeaders.append("Content-Type", "application/json")

        // 获取注册信息
        let regName = document.querySelectorAll('input')[0];
        let regId = document.querySelectorAll('input')[1];
        let regPwd = document.querySelectorAll('input')[2];
        let regBtn = document.querySelectorAll('input')[3];

        // 注册事件监听
        regBtn.addEventListener('click', () => {
            let host = 'https://db-api.amarea.cn'
            let key = 'users'
            let requestOptions = {
                method: "POST",
                headers: myHeaders,
                redirect: "follow",
            }
            let id = regId.value;
            let name = regName.value;
            let pwd = regPwd.value;
            requestOptions.body = JSON.stringify({
                id: id, name: name, password: pwd,
            });
            fetch(`${host}/${key}`, requestOptions)
                .then(response => response.json())
                .then(data => {
                    alert(data.name + '用户注册成功，欢迎您！')
                })
                .catch(err => {
                    alert('用户名已存在，注册失败，请重新输入用户名')
                    throw new Error("用户已存在，注册失败")
                })
        })

        // 获取登录信息
        let logId = document.querySelectorAll('input')[4];
        let logPwd = document.querySelectorAll('input')[5];
        let logBtn = document.querySelectorAll('input')[6];

        // 登录事件监听
        logBtn.addEventListener('click', () => {
            let requestOptions = {
                method: "GET",
                headers: myHeaders,
                redirect: "follow",
            }
            let host = 'https://db-api.amarea.cn'
            let key = 'users'
            let id = logId.value;
            let password = logPwd.value;
            fetch(`${host}/${key}/${id}`, requestOptions)
                .then(response => response.json())
                .then(data => {
                    if (data.id === id) {
                        // 密码正确跳转至记事本网页
                        // 不正确则弹出警告窗口
                        if (data.password === password) {
                            alert('用户' + data.name + '登录成功，欢迎你！')
                            window.location.href = encodeURI('./index.html?id=' + data.name);
                        } else {
                            alert('密码错误，请重新输入密码')
                            throw new Error("密码不正确")
                        }
                    } else {
                        alert("用户名不存在")
                        throw new Error("用户名不存在")
                    }
                })
                .catch(err => console.log(err))
        })
    </script>


</body>

</html>